﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Collections;
using Nortec.Security.BusinessEntities;
using Nortec.Security.BusinessComponents;
using System.Data;

namespace Nortec.Security
{
    public class NortecPrincipal : IPrincipal
    {
        private NortecIdentity identity;
        private BERol.RolDataTable roles;
        private BEPrivilegio.PrivilegioDataTable accessRights;

        public IIdentity Identity
        {
            get
            {
                return identity;
            }
            set
            {
                this.identity = (NortecIdentity)value;
            }
        }
        public BERol.RolDataTable Roles
        {
            get
            {
                return roles;
            }
        }
        public BEPrivilegio.PrivilegioDataTable AccessRights
        {
            get
            {
                return accessRights;
            }
        }

        public NortecPrincipal(NortecIdentity identity)
        {
            BCSecurity securityObject = new BCSecurity();
            roles = securityObject.GetRolesForUser(identity.Name, identity.ApplicationId);
            accessRights = securityObject.GetRightsForUser(identity.Name, identity.ApplicationId);
        }

        #region IPrincipal Members
        public bool IsInRole(string role)
        {
            DataRow[] rol = roles.Select("lit_rol_nombre = '" + role + "'");
            
            return (rol.Length > 0);
        }
        #endregion
    }
}
